package cn.net.mugui.net.pc.dialog;

import com.mugui.Dui.DPanel;

import cn.hutool.core.util.StrUtil;
import cn.net.mugui.net.key.utils.Base58;
import cn.net.mugui.net.p2p.ServerAttr;
import cn.net.mugui.net.pc.dialog.UserUpdateDialog.SERVER_TYPE;

import com.mugui.Dui.DVerticalFlowLayout;
import com.mugui.base.util.Other;

import javax.swing.JPanel;
import javax.swing.JLabel;
import com.mugui.Dui.DTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;

import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.util.encoders.Hex;

import java.awt.Color;

/**
 * 一个用户的更新处理dialog
 * 
 * @author 木鬼
 *
 */
public class UserUpdateDialog extends DPanel {
	private DTextField name;
	private DTextField address;
	private DTextField host;
	private DTextField port;
	private JComboBox<SERVER_TYPE> type;
	private DTextField status;

	public UserUpdateDialog() {
		setBackground(Color.WHITE);
		setLayout(new DVerticalFlowLayout());

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		add(panel);

		JLabel lblNewLabel = new JLabel("名称：");

		name = new DTextField(128);
		name.setColumns(32);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup().addGap(96)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(name, GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE).addGap(97)));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup().addGap(4)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
								.addComponent(name, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))));
		panel.setLayout(gl_panel);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.WHITE);
		add(panel_1);

		JLabel label = new JLabel("地址：");

		address = new DTextField(128);
		address.setColumns(32);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGap(0, 450, Short.MAX_VALUE)
				.addGroup(gl_panel_1.createSequentialGroup().addGap(96)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(address, GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE).addGap(97)));
		gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGap(0, 26, Short.MAX_VALUE)
				.addGroup(gl_panel_1.createSequentialGroup().addGap(4)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(label, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
								.addComponent(address, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))));
		panel_1.setLayout(gl_panel_1);

		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		add(panel_2);

		JLabel lblIp = new JLabel("ip：");

		host = new DTextField(128);
		host.setColumns(32);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup().addGap(96)
						.addComponent(lblIp, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(host, GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE).addGap(97)));
		gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup().addGap(4)
						.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
								.addComponent(lblIp, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
								.addComponent(host, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))));
		panel_2.setLayout(gl_panel_2);

		JPanel panel_3 = new JPanel();
		panel_3.setBackground(Color.WHITE);
		add(panel_3);

		JLabel label_2 = new JLabel("端口：");

		port = new DTextField(128);
		port.setColumns(32);
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(gl_panel_3.createParallelGroup(Alignment.LEADING).addGap(0, 450, Short.MAX_VALUE)
				.addGroup(gl_panel_3.createSequentialGroup().addGap(96)
						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(port, GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE).addGap(97)));
		gl_panel_3.setVerticalGroup(gl_panel_3.createParallelGroup(Alignment.LEADING).addGap(0, 26, Short.MAX_VALUE)
				.addGroup(gl_panel_3.createSequentialGroup().addGap(4)
						.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
								.addComponent(label_2, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
								.addComponent(port, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))));
		panel_3.setLayout(gl_panel_3);

		JPanel panel_4 = new JPanel();
		panel_4.setBackground(Color.WHITE);
		add(panel_4);

		JLabel label_3 = new JLabel("类型：");

		type = new JComboBox<>();
		type.setBackground(Color.WHITE);
		SERVER_TYPE[] values = SERVER_TYPE.values();
		for (SERVER_TYPE serType : values) {
			type.addItem(serType);
		}
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(gl_panel_4.createParallelGroup(Alignment.LEADING).addGap(0, 450, Short.MAX_VALUE)
				.addGroup(gl_panel_4.createSequentialGroup().addGap(96)
						.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(type, GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE).addGap(97)));
		gl_panel_4.setVerticalGroup(gl_panel_4.createParallelGroup(Alignment.LEADING).addGap(0, 26, Short.MAX_VALUE)
				.addGroup(gl_panel_4.createSequentialGroup().addGap(4)
						.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
								.addComponent(label_3, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
								.addComponent(type, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))));
		panel_4.setLayout(gl_panel_4);

		JPanel panel_5 = new JPanel();
		panel_5.setBackground(Color.WHITE);
		add(panel_5);

		JLabel label_1 = new JLabel("状态：");

		status = new DTextField(128);
		status.setBackground(Color.WHITE);
		status.setColumns(32);
		status.setText("已断开");
		status.setEditable(false);
		GroupLayout gl_panel_5 = new GroupLayout(panel_5);
		gl_panel_5.setHorizontalGroup(gl_panel_5.createParallelGroup(Alignment.LEADING).addGap(0, 450, Short.MAX_VALUE)
				.addGroup(gl_panel_5.createSequentialGroup().addGap(96)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(status, GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE).addGap(97)));
		gl_panel_5.setVerticalGroup(gl_panel_5.createParallelGroup(Alignment.LEADING).addGap(0, 26, Short.MAX_VALUE)
				.addGroup(gl_panel_5.createSequentialGroup().addGap(4)
						.addGroup(gl_panel_5.createParallelGroup(Alignment.LEADING)
								.addComponent(label_1, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
								.addComponent(status, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))));
		panel_5.setLayout(gl_panel_5);

	}
	/**
	 * 切换模式为新增
	 * @author 木鬼
	 */
	public void add() {
		address.setEditable(true);
	}
	/**
	 * 切换模式为编辑
	 * @author 木鬼
	 */
	public void edit() {
		address.setEditable(false);
	}
	public static enum SERVER_TYPE {
		TYPE0("未知", 0), TYPE1("公网服务器", 1), TYPE2("内网固定host,port", 2), TYPE3("内网固定host", 3), TYPE4("全动态", 4);
		private String value;

		private int type;

		private SERVER_TYPE(String value, int type) {
			this.value = value;
			this.type = type;
		}

		public String value() {
			return value;
		}

		public int type() {
			return type;
		}

		@Override
		public String toString() {
			return value;
		}
	}

	/**
	 * 服务器连接状态 1:待连接 2:连接中 3:已连接 0:已断开
	 * 
	 * @author 木鬼
	 *
	 */
	public static enum SERVER_STATUS {
		TYPE0("已断开", 0), TYPE1("待连接", 1), TYPE2("连接中", 2), TYPE3("已连接", 3);
		private String value;

		private int status;

		private SERVER_STATUS(String value, int status) {
			this.value = value;
			this.status = status;
		}

		public String value() {
			return value;
		}

		public int status() {
			return status;
		}

		@Override
		public String toString() {
			return value;
		}
	}

	@Override
	public void dataInit() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dataSave() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void quit() {
		// TODO Auto-generated method stub

	}

	ServerAttr serverAttr = null;

	public void set(ServerAttr serverAttr) {
		this.serverAttr = serverAttr;
		host.setText(serverAttr.getHost() + "");
		port.setText(serverAttr.getPort() + "");
		address.setText(serverAttr.getAddress()+"");
		name.setText(serverAttr.getName() + "");
		SERVER_TYPE[] values = SERVER_TYPE.values();
		for (SERVER_TYPE serType : values) {
			if (serType.type == serverAttr.getType()) {
				type.setSelectedItem(serType);
				break;
			}
		}
		for (SERVER_STATUS serType : SERVER_STATUS.values()) {
			if (serType.status == serverAttr.getType()) {
				status.setText(serType.value);
				break;
			}
		}
	}

	private boolean isBlank() {
		try {
			host.setText(host.getText().trim());
			port.setText(port.getText().trim());
			name.setText(name.getText().trim());
			address.setText(address.getText().trim());
			if (StrUtil.isBlank(host.getText()) || !Other.isInteger(port.getText())
					|| name.getText()==null) {
				return true;
			}
			if (StringUtils.isBlank(address.getText()) || address.getText().trim().length() != 34
					|| Base58.decode(address.getText().trim()).length != 25) {
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}

	}

	public ServerAttr get() {
		if (serverAttr == null) {
			serverAttr = new ServerAttr().setStatus(0);
		}
		if (isBlank()) {
			serverAttr = null;
			return null;
		}
		ServerAttr serverAttr1 = serverAttr.setHost(host.getText()).setPort(Integer.parseInt(port.getText()))
				.setName(name.getText()).setType(((SERVER_TYPE) type.getSelectedItem()).type)
				.setAddress(address.getText());
		for (SERVER_STATUS serType : SERVER_STATUS.values()) {
			if (serType.value.equals(status.getText().trim())) {
				serverAttr1.setStatus(serType.status);
				break;
			}
		}
		serverAttr = null;
		return serverAttr1;
	}



}
